import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 87111
 * Date: 2025-11-08
 * Time: 14:03
 */
public class Demo1 {
    public static void main(String[] args) throws InterruptedException {
        //创建10个任务
        CountDownLatch latch = new CountDownLatch(10);
        //创建线程池
        ExecutorService executor = Executors.newFixedThreadPool(5);
        for (int i = 0; i < 10; i++) {
            int id = i;
            executor.submit(() -> {
                System.out.println(Thread.currentThread().getName() + "子任务开始执行" + id);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println(Thread.currentThread().getName() + "子任务结束执行" + id);
                latch.countDown();
            });
        }
        latch.await();
        System.out.println("所有任务执行完毕");
        executor.shutdown();
    }
}
